<template>
  <div class="app-container">
    <div style="padding: 5px 0px">
      <toolbar
        :table="$refs.table"
        @add="handleEdit"
        @delete="handleDelete"
        @refresh="handleRefresh"
      >
      </toolbar>
    </div>
    <div>
      <el-table
        ref="table"
        :data="list"
        border
        fit
        :stripe="true"
        highlight-current-row
        @row-dblclick="
          (row, column, event) => {
            handleEdit(row.id);
          }
        "
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="名称">
          <template slot-scope="scope">
            {{ scope.row.userName }}
          </template>
        </el-table-column>
        <el-table-column label="邮箱" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.email }}</span>
          </template>
        </el-table-column>
        <el-table-column label="电话" align="center">
          <template slot-scope="scope">
            {{ scope.row.phoneNumber }}
          </template>
        </el-table-column>
        <el-table-column label="邮箱是否验证" align="center" v-if="false">
          <template slot-scope="scope">
            {{ scope.row.emailConfirmed }}
          </template>
        </el-table-column>
        <el-table-column label="角色" align="center">
          <template slot-scope="scope">
            {{ getRoleNames(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handleEdit(scope.row.id)"
              >{{ $t("common.edit") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="float: right">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
        :current-page="filter.pageIndex"
        :page-sizes="[10, 20, 50, 100, 200, 500]"
        :page-size="filter.pageSize"
        :prev-click="handlePrePage"
        :next-click="handleNextPage"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-drawer direction="rtl" :visible.sync="editDrawer" :with-header="false">
      <edit
        :id="currentId"
        v-if="editDrawer"
        @success="handleSave"
        @close="editDrawer = false"
      ></edit>
    </el-drawer>
  </div>
</template>

<script>
import toolbar from "@/components/Bar/toolbar.vue";
import edit from "./edit";
import { getUsers } from "@/api/user";
export default {
  components: {
    toolbar,
    edit,
  },
  data() {
    return {
      list: null,
      filter: {
        pageIndex: 1,
        pageSize: 10,
      },
      currentId: null,
      total: 0,
      editDrawer: false,
    };
  },
  mounted() {
    this.$i18n.locale = "zh_CN";
    this.$nextTick(() => {
      this.getData();
    });
  },
  methods: {
    getData() {
      getUsers({
        sorting: "name",
        skipCount: (this.filter.pageIndex - 1) * this.filter.pageSize,
        maxResultCount: this.filter.pageSize,
      }).then((res) => {
        res = res || { totalCount: 0, items: [] };
        this.list = res.items;
        this.total = res.totalCount;
      });
    },
    handlePageChange(pageIndex) {
      this.filter.pageIndex = pageIndex;
      this.getData();
    },
    handleSizeChange(pageSize) {
      this.filter.pageSize = pageSize;
      this.getData();
    },
    handleEdit(id) {
      this.currentId = id;
      this.editDrawer = true;
    },
    handleDelete({ ids }) {
      if (!ids || ids.length == 0) return;
    },
    handleRefresh() {
      this.filter.pageIndex = 1;
      this.filter.pageSize = 10;
      this.getData();
    },
    handlePrePage(currentPageIndex) {
      this.filter.pageIndex = currentPageIndex - 1;
    },
    handleNextPage(currentPageIndex) {
      this.filter.pageIndex = currentPageIndex + 1;
    },
    handleSave() {
      if (res) {
        this.showDialog = false;
        this.getData();
      }
    },
    getRoleNames(row) {
      let roleNames = row.extraProperties.RoleNames || [];
      return roleNames.toString();
    },
  },
};
</script>
